home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- from __future__ import with_statement, division
- from util import soupify, urlcacheopen, httpjoin, autoassign, httpok
- from path import path
- import subprocess
- import tarfile
- import shlex
- import logging
- log = logging.getLogger('spellchecker')
-
- class AspellDictionary(object):
-
- def __init__(self, id, name_english, name_native):
- autoassign(self, locals())
-
-
- def __repr__(self):
- return '<%s id=%s name=%s(%r)>' % (type(self).__name__, self.id, self.name_english, self.name_native)
-
-
- def name_description(self):
- return self.name_english
-
- name_description = property(name_description)
-
-
- class RemoteAspellDictionary(AspellDictionary):
- ROOT = 'http://ftp.gnu.org/gnu/aspell/dict/'
-
- def __init__(self, id, n_e, n_n, dict_dir, dict_path):
- AspellDictionary.__init__(self, id, n_e, n_n)
- self.directory = dict_dir
- self.package_path = dict_path
- self.digest_location = dict_path + '.sig'
- self.sig = None
- self.needs_update = True
-
-
- def fetch_sig(self, root = None):
- if self.sig is None:
- (response, content) = urlcacheopen(self.get_digest_location(root))
- if not response.ok:
- return False
-
- self.sig = content
- self.needs_update = not (response.fromcache)
-
- return True
-
-
- def clear_sig(self, root = None):
- pass
-
-
- def get_package_path(self, root = None):
- if not root:
- pass
- return httpjoin(self.ROOT, self.package_path)
-
-
- def get_digest_location(self, root = None):
- return self.get_package_path(root) + '.sig'
-
-
- def get_directory(self, root = None):
- if not root:
- pass
- return httpjoin(self.ROOT, self.directory)
-
-
- def __repr__(self):
- return AspellDictionary.__repr__(self)[:-1] + ' url=%s>' % self.package_path
-
-
-
- class LocalAspellDictionary(AspellDictionary):
-
- def __init__(self, id, n_e, n_n, signame):
- AspellDictionary(self, id, n_e, n_n)
- self.signame = signame
-
-
-
- def AspellIndexToYaml(root = None, outfile = None):
- import syck
- index = _GetAspellIndex(root)
- mydict = { }
-
- def RAD_to_dict(rad):
- res = { }
- if rad.name_native is not None:
- res.update(name_native = rad.name_native.encode('utf-8'))
-
- res.update(name_english = rad.name_english.encode('utf-8'), location = rad.package_path.encode('utf-8'))
- return res
-
- for d in index:
- mydict[d.id.encode('utf-8')] = RAD_to_dict(d)
-
- return syck.dump(mydict, outfile)
-
-
- def _GetAspellIndex(root = None):
- RAD = RemoteAspellDictionary
- if not root:
- pass
- (response, content) = urlcacheopen(httpjoin(RAD.ROOT, '0index.html'), decode = False)
- if not response.ok:
- print 'Unhandled HTTP response code: %r' % response
- return ()
-
- soup = soupify(content)
- results = { }
- for row in soup.find('a', attrs = dict(name = '0.50')).findAllNext('tr'):
- contents = row.findAll('td')
- if len(contents) == 4:
- (id, name_english, name_native, dictionary_path) = contents
- id = id.find(href = True)
- if id is None:
- continue
-
- id = id['href']
- if id not in results:
- dictionary_path = dictionary_path.find(href = True)
- if dictionary_path is None:
- continue
-
- dictionary_path = dictionary_path['href']
- name_english = name_english.renderContents(None).decode('xml')
- if not name_native.renderContents(None).decode('xml').strip():
- pass
- name_native = None
- results[id] = RAD(id, name_english, name_native, id, dictionary_path)
-
- id not in results
-
- return results.values()
-
-
- def DownloadAllDictionaries(infodict, towhere, root = None):
- if root is None:
- root = RemoteAspellDictionary.ROOT
-
- for id in infodict:
- name = infodict[id]['name_english']
- bz2path = infodict[id]['location']
- localpath = path(towhere) / bz2path
- bz2path = httpjoin(root, bz2path)
- localpath = localpath.expand()
- print 'Downloading %s (%s) from %s to %s... ' % (name, id, bz2path, localpath),
- (response, content) = urlcacheopen(bz2path)
- print response.reason
- if response.ok:
- if not localpath.parent.exists():
- localpath.parent.makedirs()
-
-
- try:
- f = _[2]
- f.write(content)
- finally:
- pass
-
- continue
- open(localpath, 'wb')
-
-
-
- def ExtractInfoFiles(localroot):
- localroot = path(localroot)
- for bz2path in localroot.walkfiles('*.tar.bz2'):
- tar = None
- infofile = None
-
- try:
- tar = tarfile.open(bz2path, 'r:bz2')
- for member in tar.getmembers():
- mempth = path(member.name)
- if mempth.name == 'info':
- break
- continue
- else:
- print 'Couldn\'t get "info" from %s' % bz2path
- continue
- infofile = tar.extractfile(member)
-
- try:
- f = _[2]
- f.write(infofile.read())
- finally:
- pass
-
- finally:
- if tar is not None:
- tar.close()
-
- if infofile is not None:
- infofile.close()
-
-
-
-
-
- class AspellInfoShlexer(shlex.shlex):
-
- def __init__(self, *a, **k):
- if 'posix' not in k:
- k['posix'] = True
-
- shlex.shlex.__init__(self, *a, **k)
- self.whitespace_split = True
-
-
- def get_token(self):
- curlineno = self.lineno
- token = shlex.shlex.get_token(self)
- if self.lineno != curlineno:
- self.pushback.append('\n')
-
- return token
-
-
-
- def GetAliases(root, id):
- aliases = []
- root = path(root)
-
- try:
- info = _[2]
- for line in info:
- line = line.rstrip()
- if line.startswith('alias'):
- (_alias, rest) = line.split(None, 1)
- rest = rest.split()
- alias_id = rest[0]
- alias_names = rest[1:]
- if alias_id != id:
- aliases.append((alias_id, alias_names))
-
- alias_id != id
- finally:
- pass
-
- return aliases
-
-
- def GetAllAliases(root):
- root = path(root)
- aliases = { }
- for lang in root.walkdirs():
- lang = lang.name
- aliases = GetAliases(root, lang)
- if aliases:
- print lang, aliases
- continue
-
- return aliases
-
-
- def _getshlexer(fpath):
- f = open(fpath, 'rb')
- return AspellInfoShlexer(f)
-
-
- def MakeDigsbyDict(lang = 'en', local_dir = None):
- digsby_words = set(('asap', 'asl', 'bbs', 'bff', 'brb', 'btw', 'cya', 'digsby', 'fud', 'fwiw', 'gl', 'ic', 'ily', 'im', 'imho', 'irl', 'jk', 'lmao', 'lol', 'np', 'oic', 'omg', 'plz', 'rofl', 'roflmao', 'thx', 'ttyl', 'ttys', 'u', 'wtf'))
- dict_file = local_dir / ('digsby-%s.rws' % lang)
- cmd = ' '.join([
- '.\\lib\\aspell\\bin\\aspell.exe',
- '--local-data-dir=%s' % subprocess.list2cmdline([
- local_dir]),
- '--lang=%s' % lang,
- 'create',
- 'master',
- '"%s"' % dict_file])
- startupinfo = subprocess.STARTUPINFO()
- startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
- startupinfo.wShowWindow = subprocess.SW_HIDE
- Popen = Popen
- PIPE = PIPE
- import subprocess
- proc = Popen(cmd.encode('filesys'), stdout = PIPE, stderr = PIPE, stdin = PIPE, startupinfo = startupinfo)
- log.info('Creating Digsby dict in "%s"', lang)
- log.info('Executing Command: %s', cmd)
- result = proc.communicate('\n'.join(digsby_words))
- log.info('Subprocess returned: %s', result)
- return dict_file
-
-
- def _main(*a, **k):
- MakeDigsbyDict(*a, **k)
-
- if __name__ == '__main__':
- import sys
- _main(sys.argv[1:])
-
-